<?php

namespace app\apk\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Validate;

require_once EXTEND_PATH.'MfwApi/Openapi.php'; // 载入马蜂窝api

/**
 * @title app更新（模块18）
 * @description 接口说明
 * @group 检测app版本
 * @header name:model_id   default:05 desc:模块id
 * @groupremark 已使用错误码：1130001 1130002 1130003 1130004 1130005 1130006 1130007 1130008 1130009 1130010 1130011 1130012
 */

class Index extends Controller
{


    /**
     * 测试
     *  /apk/index/test
     */
    public function test()
    {
    }

    /**
     * @title 检测app版本
     * @description 接口说明
     * @author 邓汉炜
     * @url /apk/index/index
     * @param name:token type:string require:1  other: desc:token
     * @param name:api_ver type:string require:1  other: desc:默认1.0
     * @param name:app_ver type:string require:1  other: desc:当前版本号
     * @param name:app_build_ver type:string require:1  other: desc:最新的build版本号
     * @param name:app_type type:string require:1  other: desc:设备类型
     *
     * @method ~
     */
    public function index()
    {
        $data = input();
        logs('apk',$data);
        //验证数据
        $rule = [
            'api_ver'=>'require',
            'app_ver'=>'require',
            'app_build_ver' => 'require',
            'app_type' => 'require',
        ];
        // 验证信息
        $message = [
            'api_ver.require' => '默认不能为空',
            'app_ver.require' => '默认不能为空',
            'app_build_ver.require' => '默认不能为空',
            'app_type.require' => '默认不能为空',
        ];
        $validate = new Validate($rule,$message);
        $result = $validate->check($data);
        if (!$result){
            $msg['error_id'] = 201903111903;
            $msg['message'] = $validate->getError();
            return $this->error($msg);
        }
        if ($data['app_ver'] != $data['app_build_ver']){
            switch ($data['app_type']) {
                case 'ios':
                    $msg['download_link'] = '';
                    return $this->success($msg);
                    break;
                case 'android':
                    $msg['download_link'] = '';
                    return $this->success($msg);
                    break;
            }
        }else{
            $msg['error_id'] = 201903111906;
            $msg['message'] = '无需更新';
            return $this->error($msg);
        }
    }


    //下载app
    // 默认是安卓
    //url : /apk/index/download_apk
    public function download_apk()
    {
        $data = input();
        //验证数据
        $rule = [
            'app_type' => 'require',
            'app_build_ver' => 'require',
        ];
        $message = [
            'app_type.require' => 'app类型不能为空',
            'app_build_ver.require' => 'app最新版本号不能为空',
        ];
        $validate = new Validate($rule,$message);
        $result = $validate->check($data);
        if (!$result){
            $msg['status'] = false;
            $msg['error_id'] = 20903111919;
            $msg['message'] = $validate->getError();
            return json($msg);
        }
        switch($data['app_type']){
                case 'ios':
                    $path = ROOT_PATH."public/apk/ios/{$data['app_build_ver']}.apk";
                    break;
                case 'android':
                    $path = ROOT_PATH."public/apk/android/{$data['app_build_ver']}.apk";
                    break;
            default:
                $path = ROOT_PATH."public/apk/android/{$data['app_build_ver']}.apk";
            }
            if (empty($data['app_build_ver'])){
                $data['app_build_ver'] = date('YmdHis',time()).'花儿司导app';
            }
        if (file_exists($path)){
            $fileName = "花儿户外司导端{$data['app_build_ver']}.apk";
            $fileSize = filesize($path);
            ob_clean();
            ob_flush();
            return download($path,$fileName,$fileSize,'apk');
        }else{
            $msg['status'] = false;
            $msg['error_id'] = 03121020;
            $msg['message'] = '不存在该文件';
            return json($msg);
        }
    }

    /**
     *  测试马蜂窝订单接口
     *  // 获取马峰窝订单列表
     *  返回的数据中包含 订单的总条数
     */
    public function get_mfw_order()
    {
        $action = 'sales.order.list.get'; // 调用接口
        $data = [];
        $data['page_no'] = 1; // 业务请求数据
        // 实例化请求
        $MfwOrder = new \MSDK_Php_AccessToken();
        $accessToken = $MfwOrder->getAccessToken();
        $obj = new \MSDK_Php_Openapi(); // 创建对象
        // 请求接口
        if (false == $obj->send($action,$data)){
            $msg['status'] = false;
            $msg['error_id'] = 1905051424;
            $msg['msg'] = '接口调用失败';
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            return json($msg);
        }else{
            $listData = $obj->getData()['list'];
            $total = $obj->getData()['total'];
            return json($listData);
        }
    }

    /**
     *  获取订单的详情
     * @param order_id string
     */
    public function get_mfw_order_detail()
    {
        $action = 'sales.order.detail.get'; // 调用接口
        $data = [];
        $data['order_id'] = '3279152019050514968707'; // 业务请求数据
        // 实例化请求
        $MfwOrder = new \MSDK_Php_AccessToken();
        $accessToken = $MfwOrder->getAccessToken();
        $obj = new \MSDK_Php_Openapi(); // 创建对象
        // 请求接口
        if (false == $obj->send($action,$data)){
            $msg['status'] = false;
            $msg['error_id'] = 1905051424;
            $msg['msg'] = '接口调用失败';
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            return json($msg);
        }else{
            $msg['status'] = true;
            $msg['msg'] = '接口条用成功';
            $msg['list'] = $obj->getData()['list'][0];
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            $msg['data'] = $obj->getData();
            return json($msg);
        }
    }

    /**
     *  获取订单的出行人
     * @param order_id string
     */
    public function get_mfw_order_traveller()
    {
        $action = 'sales.order.traveler.get'; // 调用接口
        $data = [];
        $data['order_id'] = '3279152019050514968707'; // 业务请求数据
        // 实例化请求
        $MfwOrder = new \MSDK_Php_AccessToken();
        $accessToken = $MfwOrder->getAccessToken();
        $obj = new \MSDK_Php_Openapi(); // 创建对象
        // 请求接口
        if (false == $obj->send($action,$data)){
            $msg['status'] = false;
            $msg['error_id'] = 1905051424;
            $msg['msg'] = '接口调用失败';
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            return json($msg);
        }else{
            $msg['status'] = true;
            $msg['msg'] = '接口条用成功';
            $msg['list'] = $obj->getData()['list'][0];
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            $msg['data'] = $obj->getData();
            return json($msg);
        }
    }

    /**
     *  获取订单备注信息
     * @param order_id string
     */
    public function get_order_notes()
    {
        $action = 'sales.order.memo.get'; // 调用接口
        $data = [];
        $data['order_id'] = '20865532019050514968308'; // 业务请求数据
        // 实例化请求
        $MfwOrder = new \MSDK_Php_AccessToken();
        $accessToken = $MfwOrder->getAccessToken();
        $obj = new \MSDK_Php_Openapi(); // 创建对象
        // 请求接口
        if (false == $obj->send($action,$data)){
            $msg['status'] = false;
            $msg['error_id'] = 1905051424;
            $msg['msg'] = '接口调用失败';
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            return json($msg);
        }else{
            $msg['status'] = true;
            $msg['msg'] = '接口条用成功';
            $msg['list'] = $obj->getData()['list'][0];
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            $msg['data'] = $obj->getData();
            return json($msg);
        }
    }

    /**
     *  获取订单点评信息列表
     * @param page_no int 默认是1
     * @url /apk/index/get_order_reviews
     */
    public function get_order_reviews()
    {
        $action = 'sales.order.comment.list.get'; // 调用接口
        $data = [];
        // 业务请求数据
        $data['page_no'] = 1;
//        $data['order_id'] = 3585982019041114354923;
        // 实例化请求
        $MfwOrder = new \MSDK_Php_AccessToken();
        $accessToken = $MfwOrder->getAccessToken();
        $obj = new \MSDK_Php_Openapi(); // 创建对象
        // 请求接口
        if (false == $obj->send($action,$data)){
            $msg['status'] = false;
            $msg['error_id'] = 1905051424;
            $msg['msg'] = '接口调用失败';
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            return json($msg);
        }else{
            $msg['status'] = true;
            $msg['msg'] = '接口调用成功';
            $msg['list'] = $obj->getData()['list'][0];
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            $msg['data'] = $obj->getData();
            $msg['total'] = count($obj->getData()['list']);
            return json($msg);
        }
    }

    /**
     *  获取订单标记颜色
     * @param order_id
     */
    public function getOrderTagColor()
    {
        $action = 'sales.order.color.get'; // 调用接口
        $data = [];
        $data['order_id'] = '20865532019050514968308'; // 业务请求数据
        // 实例化请求
        $MfwOrder = new \MSDK_Php_AccessToken();
        $accessToken = $MfwOrder->getAccessToken();
        $obj = new \MSDK_Php_Openapi(); // 创建对象
        // 请求接口
        if (false == $obj->send($action,$data)){
            $msg['status'] = false;
            $msg['error_id'] = 1905051424;
            $msg['msg'] = '接口调用失败';
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            return json($msg);
        }else{
            $msg['status'] = true;
            $mfwOrderData = $obj->getData();
            $color = '';
            if ($mfwOrderData){
                switch ($mfwOrderData['color']){
                    case 0: $color = '白色'; break;
                    case 1: $color = '红色'; break;
                    case 2: $color = '橙色'; break;
                    case 3: $color = '黄色'; break;
                    case 4: $color = '绿色'; break;
                    case 5: $color = '蓝色'; break;
                    case 6: $color = '紫色'; break;
                    case 7: $color = '灰色'; break;
                    default: $color = '白色';
                }
            }
            $msg['msg'] = '接口条用成功';
            $msg['get_msg'] = $obj->getError();
            $msg['get_error'] = $obj->getErrno();
            $msg['data'] = $color;
            return json($msg);
        }
    }
  
  /**
     * @url /apk/index/testDemo
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function testDemo()
    {
        $admin_user = Db::name('admin_user')
            ->select();

        foreach ($admin_user as $k => $v) {
            $flag = Db::name('driver_deposit')
                ->where('driver_id', $v['id'])
                ->find();

            if (!$flag) {
                switch ($v['pay_type']){
                    case 'wxpay':
                        $pay_type = '微信支付';
                        break;
                    case 'zfbpay':
                        $pay_type = '支付宝支付';
                        break;
                    case 'cashpay':
                        $pay_type = '现金支付';
                        break;
                    case 'bankpay':
                        $pay_type = '银行卡支付';
                        break;
                    case 'freightpay':
                        $pay_type = '运费中支付';
                        break;
                    default:
                        $pay_type = '';
                }
                if ($v['deposit_status'] === 'paid') {
                    Db::name('driver_deposit')
                        ->where('driver_id', $v['id'])
                        ->insert([
                            'driver_id' => $v['id'],
                            'collection_item' => '押金',
                            'collection_money' => $v['deposit'],
                            'collection_means' => $pay_type,
                            'collection_time' => $v['deposit_time'],
                            'refund_status' => 0,
                        ]);
                } elseif ($v['deposit_status'] === 'apply_refund') {
                    Db::name('driver_deposit')
                        ->where('driver_id', $v['id'])
                        ->insert([
                            'driver_id' => $v['id'],
                            'collection_item' => '押金',
                            'collection_money' => $v['deposit'],
                            'collection_means' => $pay_type,
                            'collection_time' => $v['deposit_time'],
                            'refund_status' => 4,
                            'refund_item' => '押金',
                            'refund_money' => $v['deposit'],
                            'refund_means' => $pay_type,
                            'refund_time' => '',
                            'remark' => '',
                            'reason' => '',
                        ]);
                } elseif ($v['deposit_status'] === 'refunded') {
                    Db::name('driver_deposit')
                        ->where('driver_id', $v['id'])
                        ->insert([
                            'driver_id' => $v['id'],
                            'collection_item' => '押金',
                            'collection_money' => $v['deposit'],
                            'collection_means' => $pay_type,
                            'collection_time' => $v['deposit_time'],
                            'refund_status' => 1,
                            'refund_item' => '押金',
                            'refund_money' => $v['deposit'],
                            'refund_means' => $pay_type,
                            'refund_time' => '',
                            'remark' => '',
                            'reason' => '',
                        ]);
                }
            }
        }
    }
}
